<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import useUserInfoStore from '@/stores/userInfo.js'
import { userUpdateInfoService, userUpdateAvatarService } from '@/api/user.js'
import { uploadFileService } from '@/api/upload.js'

const userInfoStore = useUserInfoStore()
const userInfo = ref({...userInfoStore.info})

// 状态和角色选项
const statusOptions = [
    { label: '正常', value: 'NORMAL' },
    { label: '禁用', value: 'DISABLED' }
]

const roleOptions = [
    { label: '普通用户', value: 'user' },
    { label: '管理员', value: 'admin' }
]

const rules = {
    nickname: [
        { required: true, message: '请输入用户昵称', trigger: 'blur' },
        {
            pattern: /^\S{2,10}$/,
            message: '昵称必须是2-10位的非空字符串',
            trigger: 'blur'
        }
    ],
    email: [
        { required: true, message: '请输入用户邮箱', trigger: 'blur' },
        { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
    ],
    userPic: [
        { required: false }
    ],
    status: [
        { required: true }
    ],
    role: [
        { required: true }
    ]
}

// 头像上传处理
const handleAvatarSuccess = async (response) => {
    userInfo.value.userPic = response.data
    try {
        await userUpdateAvatarService({ userPic: userInfo.value.userPic })
        ElMessage.success('头像更新成功')
    } catch (error) {
        ElMessage.error(error.response?.data?.message || '头像更新失败')
    }
}

// 上传前校验
const beforeAvatarUpload = (file) => {
    const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
    const isLt2M = file.size / 1024 / 1024 < 2

    if (!isJPG) {
        ElMessage.error('头像图片只能是 JPG/PNG 格式!')
    }
    if (!isLt2M) {
        ElMessage.error('头像图片大小不能超过 2MB!')
    }
    return isJPG && isLt2M
}

// 修改个人信息
const updateUserInfo = async () => {
    try {
        const result = await userUpdateInfoService(userInfo.value)
        ElMessage.success(result.msg || '修改成功')
        // 修改pinia中的个人信息
        userInfoStore.setInfo(userInfo.value)
    } catch (error) {
        ElMessage.error(error.response?.data?.message || '修改个人信息失败')
    }
}
</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>基本资料</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="userInfo" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="登录名称">
                        <el-input v-model="userInfo.username" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="用户昵称" prop="nickname">
                        <el-input v-model="userInfo.nickname"></el-input>
                    </el-form-item>
                    <el-form-item label="用户邮箱" prop="email">
                        <el-input v-model="userInfo.email"></el-input>
                    </el-form-item>
                    <el-form-item label="用户头像" prop="userPic">
                        <el-upload
                            class="avatar-uploader"
                            action="/api/upload"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                            <img v-if="userInfo.userPic" :src="userInfo.userPic" class="avatar">
                            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="用户状态" prop="status">
                        <el-select v-model="userInfo.status" placeholder="请选择状态">
                            <el-option
                                v-for="item in statusOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="用户角色" prop="role">
                        <el-select v-model="userInfo.role" placeholder="请选择角色">
                            <el-option
                                v-for="item in roleOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updateUserInfo">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>

<style scoped>
.avatar-uploader {
    width: 100px;
    height: 100px;
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}
.avatar-uploader:hover {
    border-color: var(--el-color-primary);
}
.avatar {
    width: 100%;
    height: 100%;
    display: block;
}
</style>
